package com.ruoyi.web.controller.ora;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.Builder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.AppConfig;
import com.ruoyi.ora.service.IAppConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * app设置Controller
 * 
 * @author liuwp
 * @date 2024-12-16
 */
@RestController
@RequestMapping("/ora/app_config")
public class AppConfigController extends BaseController
{
    @Autowired
    private IAppConfigService appConfigService;

    /**
     * 查询app设置列表
     */
    @PreAuthorize("@ss.hasPermi('ora:app_config:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppConfig appConfig)
    {
        startPage();
        List<AppConfig> list = appConfigService.selectAppConfigList(appConfig);
        return getDataTable(list);
    }

    /**
     * 导出app设置列表
     */
    @PreAuthorize("@ss.hasPermi('ora:app_config:export')")
    @Log(title = "app设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppConfig appConfig)
    {
        List<AppConfig> list = appConfigService.selectAppConfigList(appConfig);
        ExcelUtil<AppConfig> util = new ExcelUtil<AppConfig>(AppConfig.class);
        util.exportExcel(response, list, "app设置数据");
    }

    /**
     * 获取app设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:app_config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(appConfigService.selectAppConfigById(id));
    }

    /**
     * 新增app设置
     */
    @PreAuthorize("@ss.hasPermi('ora:app_config:add')")
    @Log(title = "app设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppConfig appConfig)
    {
        return toAjax(appConfigService.insertAppConfig(appConfig));
    }

    /**
     * 修改app设置
     */
    @PreAuthorize("@ss.hasPermi('ora:app_config:edit')")
    @Log(title = "app设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppConfig appConfig)
    {
        return toAjax(appConfigService.updateAppConfig(appConfig));
    }

    /**
     * 删除app设置
     */
    @PreAuthorize("@ss.hasPermi('ora:app_config:remove')")
    @Log(title = "app设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appConfigService.deleteAppConfigByIds(ids));
    }


    /**
     * 查询app设置
     */
    @GetMapping("/query")
    public AjaxResult configKey(String configKey)
    {
        AppConfig appConfig = appConfigService.selectOneAppConfig(Builder.of(AppConfig::new)
                .with(AppConfig::setConfigKey, configKey)
                .with(AppConfig::setUserId, SecurityUtils.getUserId())
                .build());
        return AjaxResult.success(appConfig);
    }

    @PostMapping("/setting")
    public AjaxResult setting(@RequestBody AppConfig appConfig)
    {
        if (StringUtils.isNotEmpty(appConfig.getConfigKey()) && StringUtils.isNotEmpty(appConfig.getConfigValue())){
            appConfig.setUserId(SecurityUtils.getUserId());
            AppConfig appConfig1 = appConfigService.selectOneAppConfig(Builder.of(AppConfig::new)
                    .with(AppConfig::setConfigKey, appConfig.getConfigKey())
                    .with(AppConfig::setUserId, appConfig.getUserId())
                    .build());
            if (appConfig1 == null){
                appConfigService.insertAppConfig(appConfig);
            }else {
                appConfig1.setConfigValue(appConfig.getConfigValue());
                appConfigService.updateAppConfig(appConfig1);
            }
            return AjaxResult.success(appConfig);
        }else {
            return AjaxResult.error(MessageUtils.message("ora.common.required"));
        }
    }
}
